/**
 * 条件：病区楼、楼层、房间、床位的四级联动
 * @returns
 */
$(function () {
    console.log("页面加载完成事件");
    //病区楼、楼层、房间、床位
    $.ajax({
        url: "DataTable/findWard",
        type: "post",
        dataType: "json",
        success: function (map) {
            var buildingList = map.buildingList;
            var layerList = map.layerList;
            var roomList = map.roomList;
            var bedList = map.bedList;

            var buildingList_select = document.getElementById("buildingList");
            var layerList_select = document.getElementById("layerList");
            var roomList_select = document.getElementById("roomList");
            var bedList_select = document.getElementById("bedList");

            //消除下拉框
            buildingList_select.innerHTML = "<option value='0'>选择病区楼</option>"
            layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
            roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
            bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
            //加载病区楼
            for (var i = 0; i < buildingList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = buildingList[i].wardBuildingName;
                opt.setAttribute("value", buildingList[i].wardBuildingCode);
                buildingList_select.appendChild(opt);
            }
            //给病区楼添加change事件，选择病区楼，获取楼层
            buildingList_select.onchange = function () {
                //获取病区楼ID
                var buildingLisCode = buildingList_select.value;
                //先清除原来的病区楼数据
                layerList_select.innerHTML = "<option value='0'>选择楼层</option>"
                roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区楼code获取 楼层列表(遍历病区楼去匹配)
                for (var i = 0; i < layerList.length; i++) {
                    if (layerList[i].provincecode == buildingLisCode) {
                        //var layers = layerList[i].wardLayerName;
                        var opt = document.createElement("option");
                        opt.innerText = layerList[i].wardLayerName;
                        opt.setAttribute("value", layerList[i].wardBuildingCode);
                        layerList_select.appendChild(opt);
                    }
                }
            }
            //给楼层添加change事件，选择楼层，获取病区房号
            layerList_select.onchange = function () {
                //获取楼层ID
                var layerCode = layerList_select.value;
                //先清除原来的楼层数据
                roomList_select.innerHTML = "<option value='0'>选择病区房号</option>";
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据区房号ID获取区县列表
                for (var i = 0; i < roomList.length; i++) {
                    if (roomList[i].citycode == layerCode) {
                        var opt = document.createElement("option");
                        opt.innerText = roomList[i].wardRoomName;
                        opt.setAttribute("value", roomList[i].wardLayerCode);
                        roomList_select.appendChild(opt);
                    }
                }
            }

            //给病区房号添加change事件，选择病区房号，获取床位号
            layerList_select.onchange = function () {
                //获取病区房号ID
                var roomCode = roomList_select.value;
                //先清除原来的城市数据
                bedList_select.innerHTML = "<option value='0'>选择床位号</option>";
                //根据病区房号ID获取区县列表
                for (var i = 0; i < bedList.length; i++) {
                    if (bedList[i].citycode == roomCode) {
                        var opt = document.createElement("option");
                        opt.innerText = bedList[i].wardBedName;
                        opt.setAttribute("value", bedList[i].wardRoomCode);
                        bedList_select.appendChild(opt);
                    }
                }
            }
        },
        error: function () {
            console.log("数据返回失败");
        }
    });

})
